package com.zzg.controller;


import com.alibaba.fastjson.JSONArray;
import com.zzg.entity.*;
import com.zzg.note.AdviceAnnotation;
import com.zzg.service.JurisdictionService;
import com.zzg.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zzg
 * @version 1.0.0
 * @ClassName：JurisdictionController
 * @Description: 权限管理控制器
 * @date 2021/6/18
 **/
@Controller
public class JurisdictionController {
    /**
     * 权限管理接口类
     */
    @Autowired
    private JurisdictionService jurisdictionService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 菜单界面数据展示 左边导航栏数据
     * @author zzg
     * @date 2021/6/18 14:21
     * @params request 请求作用率
     * @return 返回json格式的数据类型
     */
    @AdviceAnnotation(name = "根据登录人查询导航栏菜单")
    @RequestMapping(value = "/Menus",produces = "application/json;charset=utf-8")
    @ResponseBody
    public List<Menu> menus(HttpServletRequest request){
        //获取session作用率
        HttpSession session = request.getSession();
        TmsUser tms_user =(TmsUser)session.getAttribute("TMS_User");
        List<Menu> menu = jurisdictionService.showMenu(tms_user.getAccount());
        return menu;
    }
    /**
     * 菜单数据表格数据展示
     * @author zzg
     * @date 2021/6/18 16:42
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "查询所有角色并进行分页查询")
    @RequestMapping(value = "/RoleList",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String, Object> roleList(Integer page, Integer limit){
        return jurisdictionService.roleList(page,limit);
    }
    /**
     * 菜单数据表格数据展示
     * @author zzg
     * @date 2021/6/18 16:42
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "查询所有用户进行分页查询")
    @RequestMapping(value = "/TmsUserList",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String, Object> tmsUserList(Integer page, Integer limit){
        return jurisdictionService.tmsUserList(page,limit);
    }
    /**
     * 菜单数据表格数据展示
     * @author zzg
     * @date 2021/6/18 16:42
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "查询所有菜单进行分页查询")
    @RequestMapping(value = "/MenuList",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String, Object> menuList(Integer page, Integer limit){
        return jurisdictionService.menuList(page,limit);
    }
    /**
     * 菜单数据表格数据展示
     * @author zzg
     * @date 2021/6/18 16:42
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "查询所有菜单表中没有上级的菜单，用于添加菜单下拉框")
    @RequestMapping(value = "/PreviousMenu",produces = "application/json;charset=utf-8")
    @ResponseBody
    public List<Menu> previousMenu(){
        return jurisdictionService.previousMenu();
    }
    /**
     * 菜单数据表格数据展示
     * @author zzg
     * @date 2021/6/18 16:42
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "添加菜单，清除redis里面的缓存")
    @RequestMapping(value = "/AddMenutble",produces = "application/text;charset=utf-8")
    @ResponseBody
    public String addMenutble(Menu menu){
        menu.setMenuId(UuidUtil.getUuid());
        stringRedisTemplate.opsForHash().delete("Menu","menu");
        jurisdictionService.addMenutble(menu);
        return "添加成功";
    }
    /**
     * 根据id删除菜单
     * @author zzg
     * @date 2021/6/21 19:55
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "删除菜单，清除redis里面的缓存")
    @RequestMapping(value = "/DelMenu",produces = "application/text;charset=utf-8")
    @ResponseBody
    public String delMenu(String menuId){
        stringRedisTemplate.opsForHash().delete("Menu","menu");
        return jurisdictionService.delMenu(menuId);
    }
    /**
     * 根据id查询修改菜单需要的数据
     * @author zzg
     * @date 2021/6/21 20:04
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "菜单修改获取修改数据")
    @RequestMapping(value = "/ShowUpdateMenu",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Menu showUpdateMenu(String menuId){
        return jurisdictionService.showUpdateMenu(menuId);
    }
    /**
     * 根据id查询修改菜单需要的数据
     * @author zzg
     * @date 2021/6/21 20:04
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "根据菜单id修改菜单")
    @RequestMapping(value = "/UpdateMenu",produces = "application/text;charset=utf-8")
    @ResponseBody
    public String updateMenu(Menu menu){
        stringRedisTemplate.opsForHash().delete("Menu","menu");
        return jurisdictionService.updateMenu(menu);
    }
    /**
     * 树形组件
     * @author zzg
     * @date 2021/6/21 20:04
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "角色修改或者添加查询树形组件菜单")
    @RequestMapping(value = "/TreeForm",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String treeForm(){
        HashOperations<String, String, String> stringObjectObjectHashOperations = stringRedisTemplate.opsForHash();
        String strMenu = stringObjectObjectHashOperations.get("Menu","menu");
        if (strMenu == null) {
            //创建JSON数组
            JSONArray jsonArray = new JSONArray();
            //将集合放入JSONArray中
            jsonArray.addAll(jurisdictionService.getParent());
            //存入redis
            stringObjectObjectHashOperations.put("Menu","menu",jsonArray.toJSONString());
            return jsonArray.toJSONString();
        }
        return strMenu;
    }

    @AdviceAnnotation(name = "根据角色分配菜单栏,并向redis里面添加菜单栏")
    @RequestMapping(value = "/TreeFormText",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String, Object> treeFormText(HttpServletRequest request){
        HashOperations<String, String, String> stringObjectObjectHashOperations = stringRedisTemplate.opsForHash();

        HttpSession session = request.getSession();
        TmsUser tms_user =(TmsUser)session.getAttribute("TMS_User");
        String strMenu = stringObjectObjectHashOperations.get("Menus", tms_user.getAccount());
        //创建一个map集合
        Map<String, Object> map=new HashMap();
        //集合
        List<MenusText> list=null;

        //添加值
        map.put("code","0");
        map.put("msg","0");
        System.out.println("strMenu："+strMenu);
        if (strMenu == null) {
            //创建JSON数组
            JSONArray jsonArray = new JSONArray();
            //将集合放入JSONArray中
            jsonArray.addAll(jurisdictionService.getParentText(tms_user.getTmsId()));
            //JSON转集合
            list = JSONArray.parseArray(jsonArray.toJSONString(), MenusText.class);
            //存储数据到Redis
            stringObjectObjectHashOperations.put("Menus",tms_user.getAccount(),jsonArray.toJSONString());

        }else {
            //JSON转集合
            list = JSONArray.parseArray(strMenu, MenusText.class);

        }
        map.put("data",list);
        return map;
    }
    /**
     * 添加角色同时添加菜单角色关联表
     * @author zzg
     * @date 2021/6/21 20:04
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "添加一个角色,同时清除redis缓存")
    @RequestMapping(value = "/AddRole",produces = "application/text;charset=utf-8")
    @ResponseBody
    public String addRole(Role role,String menuS,HttpServletRequest request){
        HttpSession session = request.getSession();
        TmsUser tms_user =(TmsUser)session.getAttribute("TMS_User");
        Long menus = stringRedisTemplate.opsForHash().delete("Menus", tms_user.getAccount());
        List<RoleMenu> roleMenus=new ArrayList<>();
        //给角色赋予主键id
        role.setRoleId(UuidUtil.getUuidS());
        //判断menuS是不是空
        if (menuS != null) {
            //判断是不是有多个值
            if (menuS.contains(",")) {
                String[] split = menuS.split(",");
                for (String s : split) {
                    RoleMenu roleMenu=new RoleMenu();
                    roleMenu.setRoleId(role.getRoleId());
                    roleMenu.setMenuId(s);
                    roleMenu.setMrid(UuidUtil.getUuidS());
                    roleMenus.add(roleMenu);
                }
            }else {
                RoleMenu roleMenu=new RoleMenu();
                roleMenu.setRoleId(role.getRoleId());
                roleMenu.setMenuId(menuS);
                roleMenu.setMrid(UuidUtil.getUuidS());
                roleMenus.add(roleMenu);
            }
        }
        jurisdictionService.addRole(roleMenus,role);
        return "true";
    }

    /**
     * 树形组件
     * @author zzg
     * @date 2021/6/21 20:04
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "删除角色清除redis缓存")
    @RequestMapping(value = "/DelRole",produces = "application/text;charset=utf-8")
    @ResponseBody
    public String delRole(String roleId,HttpServletRequest request){
        HttpSession session = request.getSession();
        TmsUser tms_user =(TmsUser)session.getAttribute("TMS_User");
        Long menus = stringRedisTemplate.opsForHash().delete("Menus", tms_user.getAccount());
        return jurisdictionService.delRole(roleId);
    }
    /**
     * 修改角色数据展示
     * @author zzg
     * @date 2021/6/21 20:04
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "查询角色修改需要的数据")
    @RequestMapping(value = "/ShowUpdateRole",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Role showUpdateRole(String roleId){
        return jurisdictionService.ShowUpdateRole(roleId);
    }
    /**
     * 修改角色数据展示
     * @author zzg
     * @date 2021/6/21 20:04
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "修改人员信息")
    @RequestMapping(value = "/UpdateUser",produces = "application/json;charset=utf-8")
    @ResponseBody
    public TmsUser updateUser(String tmsId){
        return jurisdictionService.updateUser(tmsId);
    }
    /**
     * 修改角色数据展示
     * @author zzg
     * @date 2021/6/21 20:04
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "修改人员和角色信息,且清除redis")
    @RequestMapping(value = "/UpdateTmsUser",produces = "application/text;charset=utf-8")
    @ResponseBody
    public String updateTmsUser(TmsUser tmsUser,String like,HttpServletRequest request){
        HttpSession session = request.getSession();
        TmsUser tms_user =(TmsUser)session.getAttribute("TMS_User");
        Long menus = stringRedisTemplate.opsForHash().delete("Menus", tms_user.getAccount());
        List<UserRole> userRoles=new ArrayList<>();
        if (like != null) {
            if (!like.equals("")) {
                if (like.contains(",")) {
                    try {
                        String[] split = like.split(",");
                        for (String s : split) {
                            UserRole userRole=new UserRole();
                            userRole.setUserRoleId(UuidUtil.getUuidS());
                            userRole.setRoleId(s);
                            userRoles.add(userRole);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else {
                    UserRole userRole=new UserRole();
                    userRole.setUserRoleId(UuidUtil.getUuidS());
                    userRole.setRoleId(like);
                    userRoles.add(userRole);
                }
            }
        }

        return jurisdictionService.updateUserTable(userRoles,tmsUser);
    }
    /**
     * 修改角色数据展示
     * @author zzg
     * @date 2021/6/21 20:04
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "查询所有角色用于权限赋予")
    @RequestMapping(value = "/RoleCheckbox",produces = "application/json;charset=utf-8")
    @ResponseBody
    public List<Role> roleCheckbox(){
        return jurisdictionService.selectRole();
    }
    /**
     * 修改角色数据展示
     * @author zzg
     * @date 2021/6/21 20:04
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "查询人员信息并进行分页操作")
    @RequestMapping(value = "/Staff",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String, Object> staff(Integer page, Integer limit){
        return jurisdictionService.selectStaff(page,limit);
    }
    /**
     * 修改角色数据展示
     * @author zzg
     * @date 2021/6/21 20:04
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "修改角色，并清除导航菜单栏redis缓存")
    @RequestMapping(value = "/updateRole",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updateRole(Role role,String menuS,HttpServletRequest request){
        HttpSession session = request.getSession();
        TmsUser tms_user =(TmsUser)session.getAttribute("TMS_User");
        Long menus = stringRedisTemplate.opsForHash().delete("Menus", tms_user.getAccount());
        stringRedisTemplate.delete("menu");
        List<RoleMenu> roleMenus=new ArrayList<>();
        //判断menuS是不是空
        if (menuS != null) {
            //判断是不是有多个值
            if (menuS.contains(",")) {
                String[] split = menuS.split(",");
                for (String s : split) {
                    RoleMenu roleMenu=new RoleMenu();
                    roleMenu.setRoleId(role.getRoleId());
                    roleMenu.setMenuId(s);
                    roleMenu.setMrid(UuidUtil.getUuidS());
                    roleMenus.add(roleMenu);
                }
            }else {
                RoleMenu roleMenu=new RoleMenu();
                roleMenu.setRoleId(role.getRoleId());
                roleMenu.setMenuId(menuS);
                roleMenu.setMrid(UuidUtil.getUuidS());
                roleMenus.add(roleMenu);
            }
        }
        jurisdictionService.updateRole(roleMenus,role);
        return "修改成功";
    }
    /**
     * 删除用户根据id
     * @author zzg
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "根据id删除人员信息")
    @RequestMapping(value = "/deletePersonnerByid",produces = "application/text;charset=utf-8")
    @ResponseBody
    public String deletePersonner(String id){
        int i=jurisdictionService.deletePersonner(id);
        if(i>=1){
            System.out.println(i);
            return "true";
        }else{
            return "false";
        }

    }

    /**
     * 增加用户
     * @author yw
     * @params *@params
     * @return
     */
    @AdviceAnnotation(name = "增加用户")
    @RequestMapping(value = "/insertuser",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String insertuser(TmsUser tms,Personnel per){
        tms.setTmsId(UuidUtil.getUuid());
        int i=jurisdictionService.insertTmsuser(tms);
        per.setTmsUser(tms);
        per.setPersonnelName(tms.getTmsId());
        per.setPersonnelId(UuidUtil.getUuid());
        int o=jurisdictionService.insertPersonner(per);
        if(i>0&&o>0){
            return "true";
        }else{
            return "false";
        }
    }



}
